#include "FileReaderImp.h"
#include "CoderImp.h"
#include "CompressFileWriterImp.h"
#include "CompressDirector.h"
#include "DecompressDirector.h"
#include "FileWriterImp.h"
#include "DecoderImp.h"
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char* argv[])
{
	if(argc >= 3)
	{
		if(strcmp(argv[1], "-c") == 0)
		{
			char* out;
			if(4 == argc)
				out = argv[3];
			else
				out = "out";
			CompressDirector cdct(argv[2], out);
			FileReader* r = new FileReaderImp();
			Coder* c = new CoderImp();
			CompressFileWriter* w = new CompressFileWriterImp();

			if(cdct.Compress(r, c, w) == false)
				cout << "Error while compressing !" << endl;

			delete r;
			delete c;
			delete w;
		}
		else if(strcmp(argv[1], "-d") == 0)
		{
			DecompressDirector ddct(argv[2]);
			FileReader* rr = new FileReaderImp();
			Decoder* d = new DecoderImp();
			FileWriter* ww = new FileWriterImp();

			if(ddct.Decompress(rr, d, ww) == false)
				cout << "Error while decompressing !" << endl;

			delete rr;
			delete d;
			delete ww;
		}
		else
		{
			for(int i = 0; i < argc; i++)
				cout << argv[i] << endl;
		}
	}
	else
	{
		cout << "Usage: " << endl
			 << "  huffman -c soucefile outputfile" << endl
			 << "     > To code 'soucefile' and output to 'outputfile'.\n" << endl
			 << "  huffman -d codedfile" << endl
			 << "     > To decode a coded-file." << endl;
	}

	return 0;
}
